const Koa = require('koa')
const app = new Koa()
const debug = require('debug')('koa-weapp-demo')
const bodyParser = require('koa-bodyparser')

const cors = require('koa2-cors');



const response = require('./middlewares/response')
const config = require('./config')


// 使用响应处理中间件123
app.use(response)

// 解析请求体
app.use(bodyParser())




// 引入路由分发
const router = require('./routes')


app.use(cors({
	origin: function (ctx) {
		return "*"; 
	},
	exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
	maxAge: 5,
	credentials: true,
	allowMethods: ['GET', 'POST', 'DELETE'],
	allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

app.use(router.routes()).use(router.allowedMethods());




// 启动程序，监听端口
app.listen(config.port, () => debug(`listening on port ${config.port}`))
